<?php

class FormValidator {
    private $errors=array();

	public function __construct(){}

	// validate empty field
	public function validateEmpty($field,$errorMessage,$min=4,$max=32){
    	if(!isset($field) || trim($field)==''){
        	$this->errors[]=$errorMessage;
    	}
	}

	public function validateLength($field,$errorMessage,$min=4,$max=32){
    	if(strlen($field)<$min||strlen($field)>$max){
        	$this->errors[]=$errorMessage;
    	}
	}

    // validate integer field
	public function validateInt($field,$errorMessage){
    	if(!isset($field)||!is_numeric($field)||intval($field)!=$field){
        	$this->errors[]=$errorMessage;
    	}
	}

	// validate numeric field
	public function validateNumber($field,$errorMessage){
    	if(!isset($field)||!is_numeric($field)){
        	$this->errors[]=$errorMessage;
    	}
	}

	// validate if field is within a range
	public function validateRange($field,$errorMessage,$min=1,$max=99){
    	if(!isset($field) || $field<$min||$field>$max){
        	$this->errors[]=$errorMessage;
    	}
	}

    // validate alphabetic field
    public function validateAlphabetic($field,$errorMessage){
    	if(!isset($field)||!preg_match("/^[a-zA-Z]+$/",$field)){
        	$this->errors[]=$errorMessage;
    	}
	}

	public function validateAlphanum($field,$errorMessage){
    	if(!isset($field)||!preg_match("/^[a-zA-Z0-9]+$/",$field)){
        	$this->errors[]=$errorMessage;
    	}
	}

	// validate email
	public function validateEmail($field,$errorMessage){
    	if(!isset($field)||!preg_match("/.+@.+\..+./",$field)){
        	$this->errors[]=$errorMessage;
    	}
	}

    // check for errors
	public function checkErrors(){
    	if(count($this->errors)>0){
        	return true;
    	}
    	else{
    		return false;
    	}
	}

	// return errors
    public function getErrors(){
    	return $this->errors;
    }
}
?>
